//package pri.zxx.excelgenerate.excelhanler;
//
//import com.alibaba.fastjson.JSON;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.poi.hssf.usermodel.HeaderFooter;
//import org.apache.poi.hssf.usermodel.*;
//import org.apache.poi.hssf.util.HSSFColor;
//import org.apache.poi.ss.usermodel.*;
//import org.apache.poi.ss.util.CellRangeAddress;
//import pri.zxx.excelgenerate.dataentity.CommonData;
//import pri.zxx.excelgenerate.dataentity.entity.ModuleMetaInfo;
//import pri.zxx.excelgenerate.dataentity.tabledata.TableDefinition;
//import pri.zxx.excelgenerate.excelhanler.excelentity.CustomCell;
//import pri.zxx.excelgenerate.excelhanler.excelentity.CustomRow;
//
//import java.util.*;
//
///**
// * @author xigua
// * @ desc
// * @ createTime 2022/4/25 22:46
// */
//@Slf4j
//public class ExcelGenerator implements IExcelFactory {
//
//    private Map<ComponentEnum, HSSFCellStyle> cellStyleMap = new HashMap<>();
//
//    private HSSFWorkbook workbook;
//    private HSSFSheet sheet;
//
//    /**
//     * 宽度最大单元格数量  excel中索引最大为5
//     */
//    public static final Integer MAX_COLUMN_COUNT = 6;
//
//
//    //记录当前已经加载到的行数
//    private Integer rowNowCount = 0;
//
//    public ExcelGenerator() {
//        init();
//    }
//
//    public void init() {
//        String fontName = "Microsoft YaHei Light";
//        workbook = new HSSFWorkbook();
//        sheet = workbook.createSheet();
//        /* ***************************   字体样式       **************************/
//        Font font24 = workbook.createFont();
//        font24.setFontHeightInPoints((short) 24);
//        font24.setFontName(fontName);
//
//        Font font12 = workbook.createFont();
//        font12.setFontHeightInPoints((short) 10);
//        font12.setFontName(fontName);
//
//        HSSFFont moduleTitleFont = workbook.createFont();
//        moduleTitleFont.setFontHeightInPoints((short) 12);
//        moduleTitleFont.setFontName(fontName);
//        moduleTitleFont.setBold(true);
//        moduleTitleFont.setItalic(true);
//        moduleTitleFont.setColor(HSSFColor.HSSFColorPredefined.GREEN.getIndex());
//
//        /* ***************************    单元格样式      **************************/
//        HSSFCellStyle header = workbook.createCellStyle();
//        header.setWrapText(true);
//        header.setAlignment(HorizontalAlignment.CENTER);
//        header.setVerticalAlignment(VerticalAlignment.CENTER);
//        header.setFont(font24);
//        header.setBorderBottom(BorderStyle.NONE);
//        header.setBorderTop(BorderStyle.NONE);
//        header.setBorderRight(BorderStyle.NONE);
//        header.setBorderRight(BorderStyle.NONE);
//        cellStyleMap.put(ComponentEnum.HEADER_TITLE, header);
//
//        HSSFCellStyle reportNo = workbook.createCellStyle();
//        reportNo.setWrapText(true);
//        reportNo.setAlignment(HorizontalAlignment.RIGHT);
//        reportNo.setVerticalAlignment(VerticalAlignment.CENTER);
//        reportNo.setFont(font12);
//        reportNo.setBorderBottom(BorderStyle.MEDIUM);
//        cellStyleMap.put(ComponentEnum.REPORT_NO, reportNo);
//
//        HSSFCellStyle userName = workbook.createCellStyle();
//        userName.setWrapText(true);
//        userName.setAlignment(HorizontalAlignment.LEFT);
//        userName.setVerticalAlignment(VerticalAlignment.CENTER);
//        userName.setFont(moduleTitleFont);
//        userName.setBorderBottom(BorderStyle.NONE);
//        userName.setBorderTop(BorderStyle.MEDIUM);
//        userName.setBorderLeft(BorderStyle.NONE);
//        userName.setBorderRight(BorderStyle.NONE);
//        cellStyleMap.put(ComponentEnum.USER_NAME, userName);
//
//        HSSFCellStyle text = workbook.createCellStyle();
//        text.setWrapText(true);
//        text.setAlignment(HorizontalAlignment.CENTER);
//        text.setVerticalAlignment(VerticalAlignment.CENTER);
//        text.setFont(font12);
//        text.setBorderBottom(BorderStyle.THIN);
//        text.setBorderTop(BorderStyle.THIN);
//        text.setBorderRight(BorderStyle.THIN);
//        text.setBorderRight(BorderStyle.THIN);
//        cellStyleMap.put(ComponentEnum.TEXT, text);
//
//        HSSFCellStyle noBorderCell = workbook.createCellStyle();
//        noBorderCell.setWrapText(true);
//        noBorderCell.setFont(font12);
//        noBorderCell.setBorderBottom(BorderStyle.NONE);
//        noBorderCell.setBorderTop(BorderStyle.NONE);
//        noBorderCell.setBorderLeft(BorderStyle.NONE);
//        noBorderCell.setBorderRight(BorderStyle.NONE);
//        cellStyleMap.put(ComponentEnum.NO_BORDER, noBorderCell);
//
//        HSSFCellStyle first = workbook.createCellStyle();
//        first.setWrapText(true);
//        first.setAlignment(HorizontalAlignment.CENTER);
//        first.setVerticalAlignment(VerticalAlignment.CENTER);
//        first.setFont(font12);
//        first.setBorderLeft(BorderStyle.MEDIUM);
//        first.setBorderBottom(BorderStyle.THIN);
//        first.setBorderTop(BorderStyle.THIN);
//        first.setBorderRight(BorderStyle.THIN);
//        cellStyleMap.put(ComponentEnum.FIRST, first);
//
//
//        HSSFCellStyle last = workbook.createCellStyle();
//        last.setWrapText(true);
//        last.setAlignment(HorizontalAlignment.CENTER);
//        last.setVerticalAlignment(VerticalAlignment.CENTER);
//        last.setFont(font12);
//        last.setBorderRight(BorderStyle.MEDIUM);
//        last.setBorderBottom(BorderStyle.THIN);
//        last.setBorderTop(BorderStyle.THIN);
//        last.setBorderLeft(BorderStyle.THIN);
//        cellStyleMap.put(ComponentEnum.LAST, last);
//
//        HSSFCellStyle rowB_lrt = workbook.createCellStyle();
//        rowB_lrt.setWrapText(true);
//        rowB_lrt.setAlignment(HorizontalAlignment.CENTER);
//        rowB_lrt.setVerticalAlignment(VerticalAlignment.CENTER);
//        rowB_lrt.setFont(font12);
//        rowB_lrt.setBorderRight(BorderStyle.MEDIUM);
//        rowB_lrt.setBorderBottom(BorderStyle.THIN);
//        rowB_lrt.setBorderTop(BorderStyle.MEDIUM);
//        rowB_lrt.setBorderLeft(BorderStyle.MEDIUM);
//        cellStyleMap.put(ComponentEnum.ROW_B_LRT, rowB_lrt);
//
//
//        HSSFCellStyle moduleTitle = workbook.createCellStyle();
//        moduleTitle.setWrapText(true);
//        moduleTitle.setAlignment(HorizontalAlignment.LEFT);
//        moduleTitle.setVerticalAlignment(VerticalAlignment.CENTER);
//        moduleTitle.setFont(moduleTitleFont);
//        moduleTitle.setBorderRight(BorderStyle.MEDIUM);
//        moduleTitle.setBorderBottom(BorderStyle.NONE);
//        moduleTitle.setBorderTop(BorderStyle.MEDIUM);
//        moduleTitle.setBorderLeft(BorderStyle.MEDIUM);
//        cellStyleMap.put(ComponentEnum.MODULE_TITLE, moduleTitle);
//
//
//        HSSFCellStyle lrBold12 = workbook.createCellStyle();
//        lrBold12.setWrapText(true);
//        lrBold12.setAlignment(HorizontalAlignment.LEFT);
//        lrBold12.setVerticalAlignment(VerticalAlignment.CENTER);
//        lrBold12.setFont(font12);
//        lrBold12.setBorderRight(BorderStyle.MEDIUM);
//        lrBold12.setBorderBottom(BorderStyle.NONE);
//        lrBold12.setBorderTop(BorderStyle.THIN);
//        lrBold12.setBorderLeft(BorderStyle.MEDIUM);
//        cellStyleMap.put(ComponentEnum.LR_BOLD_12_AUTO_SIZE, lrBold12);
//
//        /* ***************************  初始化sheet默认值        **************************/
//        sheet.setDefaultRowHeightInPoints(18);
//        sheet.setDefaultColumnWidth(14);
//    }
//
//    @Override
//    public void handleHeader(CommonData commonData) {
//
//        //公共信息
//        List<CustomRow> reportTitle = new ArrayList<>();
//
//        /* ***************************     第一行     **************************/
//        CustomRow headerRow = new CustomRow(RowHeightConstant.ROW_HEIGHT_40);
//        headerRow.add(new CustomCell(ExcelConstant.PDF_TITLE, 0, 5, ComponentEnum.HEADER_TITLE));
//        reportTitle.add(headerRow);
//
//        /* ***************************     第二行     **************************/
//        CustomRow reportNumber = new CustomRow(RowHeightConstant.ROW_HEIGHT_22_5);
//        reportNumber.add(new CustomCell("", 0, 5, ComponentEnum.REPORT_NO));
//        reportTitle.add(reportNumber);
//
//
//        this.rowNowCount = this.loadOneRow(reportTitle, rowNowCount, false);
//
//        /* ***************************     第3行     **************************/
//        List<CustomRow> rows = new ArrayList<>();
//
//        CustomRow customRow = new CustomRow();
//        customRow.add(new CustomCell("客户名称", 0, 0, ComponentEnum.FIRST));
//        customRow.add(new CustomCell(commonData.getCustomerNameValue(), 1, 3));
//        customRow.add(new CustomCell("报告日期", 4, 4));
//        customRow.add(new CustomCell(commonData.getPdfDateValue() + "", 5, 5, ComponentEnum.LAST));
//        rows.add(customRow);
//        /* ***************************     第4行     **************************/
//
//        CustomRow exportRow2 = new CustomRow();
//        exportRow2.add(new CustomCell("饲喂批次号", 0, 0, ComponentEnum.FIRST));
//        exportRow2.add(new CustomCell(commonData.getFeedingBatchNoValue(), 1, 1));
//        exportRow2.add(new CustomCell("动物类型", 2, 2, ComponentEnum.TEXT));
//        exportRow2.add(new CustomCell(commonData.getAnimalTypeNameValue(), 3, 3, ComponentEnum.TEXT));
//        exportRow2.add(new CustomCell("品种", 4, 4));
//        exportRow2.add(new CustomCell(commonData.getVarietyNameValue(), 5, 5, ComponentEnum.LAST));
//        rows.add(exportRow2);
//        /* ***************************     第5行     **************************/
//
//        CustomRow exportRow3 = new CustomRow(RowHeightConstant.ROW_HEIGHT_30);
//        exportRow3.add(new CustomCell("营养师建议：", 0, 5, ComponentEnum.MODULE_TITLE));
//        rows.add(exportRow3);
//
//        /* ***************************     第6行     **************************/
//        CustomRow exportRow4 = new CustomRow(RowHeightConstant.ROW_HEIGHT_AUTO_SIZE);
//        exportRow4.add(new CustomCell("    " + commonData.getAdvise(), 0, 5, ComponentEnum.LR_BOLD_12_AUTO_SIZE));
//        rows.add(exportRow4);
//
//        this.rowNowCount = this.loadOneRow(rows, rowNowCount, true);
//        HSSFHeader header = sheet.getHeader();
//        header.setRight(commonData.getReportNo());
//    }
//
//
//    @Override
//    public void handleModules(List<ModuleMetaInfo> modules) {
//        modules.forEach(meta -> {
//            //行信息 列表
//            List<CustomRow> rows = new ArrayList<>();
//
//
//            //拆分数量 处理表头 标题
//            log.info("当前处理的模块信息：{}", meta.getModuleTitle());
//            //表头生成
//            rows.add(CustomRow.createRow(Collections.singletonList(CustomCell.moduleHeaderCell(meta.getModuleTitle())), RowHeightConstant.ROW_HEIGHT_30));
//
//            TableDefinition tableDefinition = meta.getTableDefinition();
//
//            List<CustomCell> ranges = tableDefinition.getRanges();
//            if (ranges != null && !ranges.isEmpty()) {
//                rows.add(CustomRow.createRow(ranges, RowHeightConstant.ROW_HEIGHT_18));
//            }
//            List<CustomCell> titles = tableDefinition.getTitles();
//            if (titles != null && !titles.isEmpty()) {
//                rows.add(CustomRow.createRow(titles, RowHeightConstant.ROW_HEIGHT_30));
//            }
//            List<CustomCell> data = tableDefinition.getData();
//            if (data != null && !data.isEmpty()) {
//                rows.add(CustomRow.createRow(data, RowHeightConstant.ROW_HEIGHT_18));
//
//            }
//            List<CustomRow> rowsData = tableDefinition.getRowsData();
//            if (rowsData != null && !rowsData.isEmpty()) {
//                rows.addAll(rowsData);
//            }
//            this.rowNowCount = this.loadOneRow(rows, rowNowCount, true);
//        });
//        CustomRow userName = new CustomRow(RowHeightConstant.ROW_HEIGHT_18);
//        userName.add(new CustomCell("报告人：" + "钢蛋", 0, 5, ComponentEnum.NO_BORDER));
//        this.rowNowCount = this.loadOneRow(Collections.singletonList(userName), rowNowCount, true);
//
//    }
//
//    /**
//     * 处理文档结尾
//     */
//    public void handleFooter(CommonData commonData) {
//        /* ***************************     最后一行     **************************/
//        CustomRow reportNumber = new CustomRow(RowHeightConstant.ROW_HEIGHT_18);
//        reportNumber.add(new CustomCell("   报告人： ", 0, 5, ComponentEnum.USER_NAME));
//        this.rowNowCount = this.loadOneRow(Collections.singletonList(reportNumber), rowNowCount, false);
//    }
//
//    @Override
//    public void handleGloabalStyle() {
//        //        ExcelUtil.insertPic(workbook, sheet, ExcelConstant.LOGO_PATH);
//
//        /* ***************************  处理默认值        **************************/
//
////        sheet.setMargin(Sheet.LeftMargin, 0.5);// 页边距（左）
////        sheet.setMargin(Sheet.RightMargin, 0.3);// 页边距（右）
//
////        sheet.setMargin(Sheet.TopMargin, 0.3);// 页边距（上）
//
//        sheet.setHorizontallyCenter(true);//设置打印页面为水平居中
//
////        sheet.setVerticallyCenter(true);//设置打印页面为垂直居中
//        HSSFFooter footer = sheet.getFooter();
//        footer.setCenter("--   " + HeaderFooter.page() + "  --");
//        //使用POI输出Excel时打印页面
//        HSSFPrintSetup printSetup = sheet.getPrintSetup();
//
////        printSetup.setLandscape(false); // 打印方向，true：横向，false：纵向
//
//        printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //纸张大小，自选A4
//    }
//
//    @Override
//    public HSSFWorkbook outPutExcel() {
//        return  workbook;
//    }
//
//    /**
//     * 组装一行
//     *
//     * @return 当前行数
//     */
//    public int loadOneRow(List<CustomRow> exportRows, Integer rowNowCount, boolean defaultStyle) {
//
//        for (CustomRow exportRow : exportRows) {
//            log.info("【{}】开始组装:{}", rowNowCount, JSON.toJSONString(exportRow));
//            List<CustomCell> list = exportRow.getList();
//            HSSFRow row = sheet.createRow(rowNowCount);
//
//            if (exportRow.getRowHeight() != 0) {
//                row.setHeightInPoints(exportRow.getRowHeight());
//            }
//            boolean autoSize = exportRow.getRowHeight() == 0;
//            short autoSizeMaxHeight = 0;
//            for (CustomCell c : list) {
//                HSSFCell cell = row.createCell(c.getStart());
//                HSSFCellStyle xcs = cellStyleMap.get(c.getComponentEnum());
////                cell.setCellValue(c.getValue());
//                HSSFRichTextString hrts = new HSSFRichTextString(c.getValue());
//                cell.setCellValue(hrts);
//
//                //先初始化格式
//                if (c.getEnd() == ExcelGenerator.MAX_COLUMN_COUNT - 1 && c.getStart() == ExcelGenerator.MAX_COLUMN_COUNT - 1) {
//                    cell.setCellStyle(cellStyleMap.get(ComponentEnum.LAST));
//                } else if (c.getStart() == 0 && c.getEnd() == 0) {
//                    cell.setCellStyle(cellStyleMap.get(ComponentEnum.FIRST));
//                } else {
//                    cell.setCellStyle(xcs);
//                }
//
//                if (autoSize) {
//                    short i = (short) ExcelUtil.computeHeight(cellStyleMap.get(c.getComponentEnum()), workbook, c.getValue());
//                    if (i > autoSizeMaxHeight) {
//                        autoSizeMaxHeight = i;
//                    }
//                    row.setHeightInPoints(autoSizeMaxHeight);
//                }
//                //合并单元格，合并后初始化格式会被刷新
//                if (c.getEnd() - c.getStart() > 0) {
//                    CellRangeAddress region = new CellRangeAddress(rowNowCount, rowNowCount, c.getStart(), c.getEnd());
//                    ExcelUtil.RangeBorder rangeBorder = ExcelUtil.RangeBorder.builder().build().setRegion(region).setSheet(sheet).setT(BorderStyle.valueOf(xcs.getBorderTop())).setB(BorderStyle.valueOf(xcs.getBorderBottom()));
//                    //默认border格式，左右 加粗
//                    if (defaultStyle) {
//                        if (c.getStart() == 0) {
//                            rangeBorder.setL(BorderStyle.MEDIUM);
//                        } else {
//                            rangeBorder.setL(BorderStyle.valueOf(xcs.getBorderLeft()));
//                        }
//                        if (c.getEnd() == ExcelGenerator.MAX_COLUMN_COUNT - 1) {
//                            rangeBorder.setR(BorderStyle.MEDIUM);
//                        } else {
//                            rangeBorder.setR(BorderStyle.valueOf(xcs.getBorderRight()));
//                        }
//                    } else {
//                        //非默认， 利用传入的style生成
//                        rangeBorder.setL(BorderStyle.valueOf(xcs.getBorderLeft())).setR(BorderStyle.valueOf(xcs.getBorderRight()));
//                    }
//                    ExcelUtil.handleRangeBorder(rangeBorder);
//                }
//            }
//            rowNowCount++;
//        }
//
//        return rowNowCount;
//    }
//}
